package com.telnetnz.ticketdesk.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.bean.Member;
import com.telnetnz.ticketdesk.common.config.SpringContext;
import com.telnetnz.ticketdesk.dao.entity.MemberEntity;
import com.telnetnz.ticketdesk.dao.wrapper.MemberWrap;

public class MemberService
{
	public static final String[] ORDER_BY = MemberWrap.ORDER_BY;
	public static final int ORDERBY_ID = 0;
	public static final int ORDERBY_NAME = 1;
	public static final int ORDERBY_EMAIL = 2;
	public static final int ORDERBY_CREATETIME = 3;

	public static final String[] ORDER = MemberWrap.ORDER;

	public static final String[] STATUSES = MemberWrap.STATUSES;
	public static final int STATUS_NORMAL = 0;
	public static final int STATUS_INACTIVATED = 1;

	private static Logger logger = Logger.getLogger(MemberService.class);
	private static String CACHE_KEY = "id_";

	/**
	 * Important: This is only for comparison with the updated bean
	 * 
	 * @param uid
	 * @return
	 */
	public static Member getRawMember(long uid)
	{
		MemberEntity entity = getWrap().select(uid);
		return Member.adapt(entity);
	}

	/**
	 * Important: This is the only entry to get the member bean, including the methods that return instances list
	 * 
	 * @param uid
	 * @return
	 */
	public static Member getMember(long id)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY + id);
		Member bean;
		if (elm == null)
		{
			MemberEntity entity = getWrap().select(id);
			bean = Member.adapt(entity);
			getWrap().getCache().put(new Element(CACHE_KEY + id, bean));
		}
		else
		{
			bean = (Member) elm.getValue();
		}
		return bean;
	}

	public static void add(Member member)
	{
		if (member == null)
			return;
		getWrap().insert(member.toEntity());
		getWrap().getCache().remove(CACHE_KEY + member.getId());
	}

	public static void update(Member member)
	{
		if (member == null)
			return;
		member.setUpdateTime(new Date());
		getWrap().update(member.toEntity());
		getWrap().getCache().remove(CACHE_KEY + member.getId());
	}

	public static void delete(Member member)
	{
		if (member == null)
			return;
		getWrap().delete(member.toEntity());
		getWrap().getCache().remove(CACHE_KEY + member.getId());
	}

	public static Member getMemberByName(String name)
	{
		long id = getWrap().selectByName(name);
		return getMember(id);
	}

	public static Member getMemberByEmail(String email)
	{
		long id = getWrap().selectByEmail(email);
		return getMember(id);
	}

	/**
	 * 根据name和email的过滤条件, 页面起始和页面大小, 获取member列表
	 * 
	 * @param start
	 * @param limit
	 * @param orderby
	 * @param name_key
	 * @param email_key
	 * @return
	 */
	public static List<Member> getByPageAndCondition(
			int start,
			int limit, 
			int orderby, 
			int order,
			String name,
			String email,
			int status,
			int excludedStatus)
	{
		List<Long> ids = getWrap().selectByPageAndCondition(
				start, limit, orderby, order, name, email, status, excludedStatus);
		return adaptIdsList(ids);
	}

	public static long countByCondition(
			String name,
			String email,
			int status,
			int excludedStatus)
	{
		return getWrap().countByCondition(name, email, status, excludedStatus);
	}

	private static List<Member> adaptIdsList(List<Long> ids)
	{
		List<Member> members = new ArrayList<Member>();
		if (ids != null && ids.size() > 0)
		{
			for (Long id : ids)
			{
				Member member = getMember(id);
				if (member != null)
					members.add(member);
			}
		}
		return members;
	}

	private static MemberWrap getWrap()
	{
		return SpringContext.getMemberWrap();
	}

	public static void main(String[] args)
	{
		
	}
}
